<?php

namespace Admin\Controller;

use \Admin\Controller\AdminController;

/*
 * 接口管理类
 */

class ApiController extends AdminController {
    /**
     * 查询接口
     * @author 刘新
     */
    public function apiList() {
        $con             = I('condition');
        $where['status'] = array('egt',0);

        if($con) {
            if(preg_match('/^[1-9]\d*$/',$con)) {
                $where['id'] = $con;
            }
            else if(preg_match('/^_[\w]{2,}$/',$con)) {
                $where['code'] = $con;
            }
        }

        if(I('status','','/^-?\d$/') !== '') {
            $where['status'] = I('status');
        }

        $list = $this->lists('Api',$where,'id',"");
        int_to_string($list);
        $this->assign('_list',$list);
        $this->display('list');
    }

    /**
     * 标记删除的API列表
     * @author 刘新
     */
    public function deleteList() {
        if(I('status','','/^-1$/') === '')
            $this->error('参数错误');
        $this->apiList();
    }

    /**
     * 添加接口
     * @author 刘新
     */
    public function addApi() {
        if(IS_GET) {
            $this->display('add_edit');
        }

        //添加API
        $res = D('Api')->addApi();
        if($res['error'] != 0)
            $this->error($res['msg']);

        $this->assign('_title','新增API');
        $this->success('添加成功',U('Api/apiList'));
    }

    /**
     * 编辑接口
     * @author 刘新
     */
    public function editApi() {
        $id = I('id','','intval');
        !$id && $this->error('请选择要编辑的API');

        //如果是GET请求，则导入编辑界面
        if(IS_GET) {
            $api = D('Api')->where(array('id' => $id))->find();
            !$api && $this->error('查询失败，请确认要编辑的API是否存在');
            //die(json_encode($api));
            $this->assign('_title','编辑API');
            $this->assign('api',$api);
            $this->display('add_edit');
        }
        //编辑API
        $res = D('Api')->editApi();

        if($res['error'] != 0)
            $this->error($res['msg']);
        $this->success('编辑成功',U('Api/apiList'));
    }

    /**
     * 接口详情
     * @author 刘新
     */
    public function detailApi() {
        $id   = I('id');
        $code = I('apiCode');

        if(!$id && !$code) {
            $this->error('参数错误');
        }

        $res  = D('Api')->find($id);
        !$res && $this->error('查询失败');
        $note = $this->returnList('GlobalNote',array('super' => 1));
        !$note && $this->error('备注查询失败');

        foreach($note as $key => $val) {
            if($val['type'] == 2) {
                $res['api_sign_note'] = $val['content'];
                continue;
            }

            if($val['type'] == 1) {
                $res['api_sysparam_note'] = $val['content'];
                continue;
            }
        }

        $config_api_request = C('API_REQUEST');
        $res['api_method_text'] = $config_api_request[$res['api_method']];

        $res                    = array($res);
        $this->assign('api',$res[0]);
        $this->assign('note',$note);
        $this->display('detail');
    }

    /**
     * API状态修改
     * @author 刘新
     */
    public function changeStatus($method = null) {
        $id         = array_unique((array)I('id',0));
        $id         = is_array($id) ? implode(',',$id) : $id;
        if(empty($id))
            $this->error('请选择要操作的数据!');
        $map['uid'] = array('in',$id);

        switch(strtolower($method)) {
            case 'forbidapi':
                $this->forbid('Api',$map);
                break;
            case 'resumeapi':
                $this->resume('Api',$map);
                break;
            case 'deleteapi':
                $this->delete('Api',$map);
                break;
            default:
                $this->error('参数非法');
        }
    }

    /**
     * 删除API
     * @author 刘新
     */
    public function clearApi() {
        $this->clear('Api');
    }

    /**
     * 缓存API
     * @author 刘新
     */
    public function cacheApi() {
        $api = $this->returnList('Api','','','id,code,name,module,controller,action,method');
        if(!$api)
            $this->ajaxReturn(array('error' => 1,'msg' => '更新失败'));

        //将code作为每个接口的键名缓存
        $tmp = array();
        foreach($api as $value) {
            $tmp[$value['code']] = $value;
        }

        $api = $tmp;
        S(C('API_CACHE_NAME'),$api,array('expire' => 365 * 24 * 60 * 60));
        $this->ajaxReturn(array('error' => 0,'msg' => '更新成功'));
    }

}
